/*** Trigger Name: fulfillmentFunctionDefinition
 * Author: Accenture
 * Date: 06-Jun-2011
 * Requirement/Project Name: iFulfill
 * Requirement Description: When ever Function name is updated in the 
 *                          Function pre-definition object then assoicated workflow's Function name will be modified.
 **/
 trigger fulfillmentFunctionDefinitionTrigger on Stage_Definition__c (after update){

  list<Workflow_Stage__c> workflowStageList = new list<Workflow_Stage__c>();
   Map<id,String> stageMap = new Map<id,string>();

   if (Trigger.isUpdate) {
        for(Stage_Definition__c SD :trigger.new){ 
          if(SD.Name != Trigger.oldMap.get(SD.id).Name){
              stageMap.put(SD.id, SD.name);
          }
        }
        System.debug('StageMap List'+stageMap.keyset());
        for(Workflow_Stage__c WS :[select Id,Name,Stage__c, stage__r.name from Workflow_Stage__c where Stage__c = :stageMap.keyset()]){                         
            System.debug('Workflow Stage value'+WS);
            WS.Name=stageMap.get(WS.Stage__c);
            workflowStageList.add(WS);
        }            
     }
     try{if(workflowStageList.size()>0){update workflowStageList;}        
     }catch(Exception e){
        system.debug(e); 
        GeneralConstants.mailAnyExceptionToMe('FulfillmentFunctionDefinitionTrigger', 'Update Functionname' , e);
     }
 }